<template>
  <div class="login_container">
    <div class="login_box">
      <!--头像区-->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="">
      </div>
      <!--登陆区-->
      <el-form ref="loginFormRef" :rules="loginRule" label-width="0" class="login_form" :model="loginForm">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="el-icon-user-solid"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="loginForm.password" prefix-icon="el-icon-edit"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login()">提交</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'Login',
    data() {
      return {
        loginForm: {
          username: 'admin',
          password: '123456'
        },
        loginRule: {
          username: [
            { required: true, message: "请输入登陆名称", trigger: "blur"},
            { min: 3, max: 10, message: "长度在3到10个字符之间", trigger: "blur"}
          ],
          password: [
            { required: true, message: "请输入用户密码", trigger: "blur"},
            { min: 6, max: 10, message: "长度在6到10个字符之间", trigger: "blur"}
          ]
        }
      }
    },
    methods: {
      resetLoginForm(){
        this.$refs.loginFormRef.resetFields();
      },
      login(){
        const axios = require('axios');
        const _this = this;
        this.$refs.loginFormRef.validate(async (valid)=> {
          // if (!valid) return;
          // const { data: res } = await this.$axios.post("login", this.loginForm);
          // if (res.meta.status !== 200) return console.log('登陆失败')
          // console.log('登陆成功')
          if (!valid){
            return;
          }
          const { data: res } = await axios.post("http://localhost:8181/user/login", _this.loginForm);
          // this.$message.success('登陆成功');
          // window.sessionStorage.setItem('token', '123456')
          // this.$router.push('/home')
          if (res === true){
            this.$message.success('登陆成功');
            window.sessionStorage.setItem('token', _this.loginForm);
            this.$router.push('/home');
          }
          console.log(res)
        })
      }
    }
  }

</script>

<style lang="less" scoped>
  .login_container{
    background-color: #2b4b6b;
    height: 100%;
  }
  .login_box{
    width: 450px;
    height: 300px;
    background-color: aliceblue;
    border-radius: 10px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .avatar_box{
      height: 130px;
      width: 130px;
      border: 1px solid white;
      border-radius: 50%;
      padding: 10px;
      position: absolute;
      left: 50%;
      right: 50%;
      transform: translate(-50%, -50%);
      background-color: lightgrey;
      img{
        height: 100%;
        width: 100%;
        border-radius: 50%;
        background-color: lightgrey;
      }
    }
  }

  .login_form{
    width: 100%;
    padding: 20px;
    position: absolute;
    box-sizing: border-box;
    bottom: 0;
  }

  .btns{
    display: flex;
    justify-content: flex-end;
  }

</style>
